var express = require('express');
var path = require('path');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var usocket = [];

// 设置静态文件
app.use('/comm', express.static(path.join(__dirname, 'comm')));
app.use('/dplayer', express.static(path.join(__dirname, 'dplayer')));

// 路由页面
app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});

// 监听 dplayer  发送弹幕
app.all('/dplayer/v3', function (req, res) {
    res.send({
        code: 200,
        msg: '发送弹幕成功',
    });
});

// socket 监听连接成功
io.of('/some').on('connection', function (socket) {
    console.log('a user connected')


    // 监听 通过socket 加入人员
    socket.on("join", function (name) {
        usocket[name] = socket
        io.of('/some').emit("join", name)
    })


    //监听 通过socket 发送消息
    socket.on("message", function (msg) {
        io.of('/some').emit("message", msg) //将新消息广播出去
    })
});

http.listen(3000, function () {
    console.log('listening on *:3000');
});
